from enum import Enum
from mcpy.decorators import reflectable, method, module
from dataclasses import field


@module("mc.protocol")
class ProtocolModule:
    pass


@reflectable("MessageType", module=ProtocolModule)
class message_type(Enum):
    REQUEST = 0
    RESPONSE = 1
    NOTIFICATION = 2


@reflectable("ProtocolVersion", module=ProtocolModule)
class protocol_version(Enum):
    V1_0 = 0
    V1_1 = 1
    V2_0 = 2


@reflectable("ProtocolHandler", module=ProtocolModule)
class ProtocolHandler:
    _version: int = field(default=0, name="Version")
    _last_request: str = field(default="", name="LastRequest")
    _last_response: str = field(default="", name="LastResponse")
    _note_count: int = field(default=0, name="NoteCount")

    def __init__(self) -> None:
        self._version: int = 0
        self._last_request: str = ""
        self._last_response: str = ""
        self._note_count: int = 0

    @method(name="SetVersion")
    def set_version(self, v: int) -> None:
        self._version = v

    @method(name="GetVersion")
    def get_version(self) -> int:
        return self._version

    @method(name="HandleRequest")
    def handle_request(self, req: str) -> str:
        self._last_request = req
        return "Processed request: " + req

    @method(name="HandleResponse")
    def handle_response(self, resp: str) -> bool:
        self._last_response = resp
        return resp != ""

    @method(name="SendNotification")
    def send_notification(self, msg: str) -> None:
        self._note_count += 1

    @method(name="GetNotificationCount")
    def get_notification_count(self) -> int:
        return self._note_count

    @method(name="GetLastRequest")
    def get_last_request(self) -> str:
        return self._last_request

    @method(name="GetLastResponse")
    def get_last_response(self) -> str:
        return self._last_response

    @method(name="ClearMessages")
    def clear_messages(self) -> None:
        self._last_request = ""
        self._last_response = ""
        self._note_count = 0

    @method(name="GetVersionString")
    def get_version_string(self) -> str:
        return "1.0" if self._version == 0 else ("1.1" if self._version == 1 else "2.0")
